package com.imy.lua.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.ClipboardUtils;
import com.imy.lua.code.ColorCodeMaker;
import com.imy.common.util.ColorUtils;
import com.imy.common.util.ToastUtil;
import com.imy.lua.R;

public class ManualColorView extends BaseCustomViewGroup {
    private TextView logTv;
    private View colorView;

    public ManualColorView(Context context) {
        super(context);
    }

    public ManualColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ManualColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        colorView = inflate.findViewById(R.id.color_view);
        logTv = inflate.findViewById(R.id.log_tv);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                tryToGetColor(event);
                return true;
            }
        });
    }

    private void tryToGetColor(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int rawX = (int) event.getRawX();
                int rawY = (int) event.getRawY();
                if (listener != null) {
                    listener.onMove(rawX, rawY);
                }
                break;
            case MotionEvent.ACTION_UP:
                upX = (int) event.getRawX();
                upY = (int) event.getRawY();
                if (listener != null) {
                    listener.onUp(upX, upY);
                }
                break;
        }
    }

    private int upX;
    private int upY;
    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_manual_color;
    }

    public void show() {
        setVisibility(VISIBLE);
    }

    public void showColor(int color, int x, int y) {
        String s = ColorUtils.integerToHex(color);
        logTv.setText("当前颜色:" + s + " x:" + x + " y:" + y);
        colorView.setBackgroundColor(color);
    }

    public void hide() {
        setVisibility(GONE);
    }

    public void makeCode(Bitmap mScreenBitmap) {
        int pixel = mScreenBitmap.getPixel(upX, upY);
        String s = ColorUtils.integerToHex(pixel);
        String colorCode = ColorCodeMaker.getColorCode(s,upX,upY);
        ClipboardUtils.setText(mContext,colorCode);
        ToastUtil.toast(mContext,"复制取色代码到粘贴板成功");
    }

    public interface ManualListener {
        void onMove(int x, int y);

        void onUp(int x, int y);
    }

    public void setManualListener(ManualListener manualListener) {
        this.listener = manualListener;
    }

    private ManualListener listener;
}
